/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.yxw.live_vod_boot.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import com.volcengine.model.live.request.ForbidStreamRequest;
import com.volcengine.model.live.request.ResumeStreamRequest;
import com.volcengine.model.live.response.ForbidStreamResponse;
import com.volcengine.model.live.response.ResumeStreamResponse;
import com.volcengine.model.live.v20230101.DescribeClosedStreamInfoByPageQuery;
import com.volcengine.model.live.v20230101.DescribeClosedStreamInfoByPageRes;
import com.volcengine.model.live.v20230101.DescribeForbiddenStreamInfoByPageQuery;
import com.volcengine.model.live.v20230101.DescribeForbiddenStreamInfoByPageRes;
import com.volcengine.model.live.v20230101.DescribeLiveStreamInfoByPageQuery;
import com.volcengine.model.live.v20230101.DescribeLiveStreamInfoByPageRes;
import com.volcengine.model.live.v20230101.KillStreamBody;
import com.volcengine.model.live.v20230101.KillStreamRes;
import com.volcengine.service.live.LiveService;
import com.volcengine.service.live.impl.LiveServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import com.yxw.yxnet_cd_center.common.utils.BaseResult;
import cn.hutool.core.util.StrUtil;
/**
 * 直播流火山处理逻
 * @author hjx
 * @date 2023-12-7
 */
@Component
@Slf4j
public class LiveStreamUtil {
    //火山引擎账号密钥ID
    @Value("${volcano.access_key_id:AKLTZGYxOWU3MGQyNTM2NDMyNTgzODExNjRhZTU1NDlmODk}")
    private static String accessKeyId;

    //火山引擎账号密钥key
    @Value("${volcano.secret_access_key:TVdZMlptSTBaV0ZoTmpjMk5EUmxNemxoWkRjM1lqSTVOVFJtWTJWaE1XUQ==}")
    private static String secretAccessKey;
    
    /**
     * 禁播直播流
     *
     * @param domainName
     * @param appName
     * @param streamName
     */
    public static BaseResult forbidStream(String domainName,String appName,String streamName) {
        LiveService liveService = LiveServiceImpl.getInstance();
        liveService.setAccessKey(accessKeyId);
        liveService.setSecretKey(secretAccessKey);        
        ForbidStreamRequest request = new ForbidStreamRequest();
        request.setDomain(domainName);
        request.setApp(appName);
        request.setStream(streamName);
        try 
        {
            ForbidStreamResponse response = liveService.forbidStream(request);
            System.out.println(JSON.toJSONString(response));
            if(null != response.getResponseMetadata().getError())
            {
                return BaseResult.failed(response.getResponseMetadata().getError().getCode(),response.getResponseMetadata().getError().getMessage());
            }
            else
            {
                return BaseResult.success(response);
            }
        } 
        catch (Exception e) 
        {
            //e.printStackTrace();
            return BaseResult.failed(e.getMessage());
        }
    }

    /**
     * 恢复禁播直播流
     *
     * @param domainName
     * @param appName
     * @param streamName
     */
    public static BaseResult resumeStream(String domainName,String appName,String streamName) {
        LiveService liveService = LiveServiceImpl.getInstance();
        liveService.setAccessKey(accessKeyId);
        liveService.setSecretKey(secretAccessKey);  
        ResumeStreamRequest request = new ResumeStreamRequest();
        request.setDomain(domainName);
        request.setApp(appName);
        request.setStream(streamName);
        try 
        {
            ResumeStreamResponse response = liveService.resumeStream(request);
            System.out.println(JSON.toJSONString(response));
            if(null != response.getResponseMetadata().getError())
            {
                return BaseResult.failed(response.getResponseMetadata().getError().getCode(),response.getResponseMetadata().getError().getMessage());
            }
            else
            {
                return BaseResult.success(response);
            }
        }
        catch (Exception e) 
        {
            //e.printStackTrace();
            return BaseResult.failed(e.getMessage());
        }
    } 
    /**
     * 中断直播流
     *
     * @param vHost
     * @param appName
     * @param streamName
     */
    public static BaseResult KillStream(String vHost,String appName,String streamName)
    {
        com.volcengine.service.live.v20230101.LiveService liveService = com.volcengine.service.live.v20230101.LiveService.getInstance();
        liveService.setAccessKey(accessKeyId);
        liveService.setSecretKey(secretAccessKey);
        KillStreamBody body = new KillStreamBody();
        body.setVhost(vHost);
        body.setApp(appName);
        body.setStream(streamName);
        try
        {
            KillStreamRes response = liveService.killStream(body);
            System.out.println(JSON.toJSONString(response));
            if(null != response.getResponseMetadata().getError())
            {
                return BaseResult.failed(response.getResponseMetadata().getError().getCode(),response.getResponseMetadata().getError().getMessage());
            }
            else
            {
                return BaseResult.success(response);
            }
        }
        catch (Exception e) 
        {
            //e.printStackTrace();
            return BaseResult.failed(e.getMessage());
        }    
    }
    /**
     * 查询在线流
     *
     * @param vHost
     * @param domainName
     * @param appName
     * @param streamName
  
     * @param pageNum 
     * @param pageSize
     */
    public static BaseResult DescribeLiveStreamInfoByPage(String vHost,String domainName,String appName,String streamName,int pageNum,int pageSize)
    {
        //String queryType,String streamType,String sourceType
        //* @param QueryType   fuzzy：模糊匹配；strict：精准匹配。
        //* @param StreamType  origin：原始流；trans：转码流。
        //* @param SourceType  push：直推流 relay：回源流   
        //?Action=DescribeLiveStreamInfoByPage&Version=2023-01-01&PageNum=1&PageSize=10&Vhost=live.example.net&Domain=live.example.net&App=live&Stream=sumasTest&StreamType=origin&SourceType=push&QueryType=fuzzy
        if(pageNum <1)
        {
            pageNum = 1;
        }
        if(pageSize <1)
        {
            pageSize = 10;
        }
        com.volcengine.service.live.v20230101.LiveService service = com.volcengine.service.live.v20230101.LiveService.getInstance();
        service.setAccessKey(accessKeyId);
        service.setSecretKey(secretAccessKey);
        DescribeLiveStreamInfoByPageQuery query = new DescribeLiveStreamInfoByPageQuery();
        query.setVhost(vHost);
        query.setApp(appName);
        query.setDomain(domainName);
        if(StrUtil.isNotBlank(streamName))
        {
            query.setStream(streamName);
        }
        query.setStreamType("origin");
        query.setSourceType("push");
        query.setPageNum(pageNum);
        query.setPageSize(pageSize);
        try
        {
            DescribeLiveStreamInfoByPageRes resp = service.describeLiveStreamInfoByPage(query);
            System.out.println(resp);
            return BaseResult.success(resp);
        } 
        catch (Exception e)
        {
            e.printStackTrace();
            return BaseResult.failed(e.getMessage());
        }
        
    }
    /**
     * 查询历史流列表
     *
     * @param vHost
     * @param domainName
     * @param appName
     * @param streamName     
     * @param SourceType  push：直推流 relay：回源流
     * @param QueryType   fuzzy：模糊匹配；strict：精准匹配。
     * @param sort  asc：升序；desc：降序。
     * @param EndTimeFrom 查询的起始时间
     * @param EndTimeTo 查询的结束时间
     * @param pageNum 
     * @param pageSize
     */
    public static BaseResult DescribeClosedStreamInfoByPage(String vHost,String domainName,String appName,String streamName,String sort,String EndTimeFrom,String EndTimeTo,int pageNum,int pageSize)
    {   
        //String sourceType,String queryType
        //https://live.volcengineapi.com?Action=DescribeClosedStreamInfoByPage&Version=2023-01-01&PageNum=1&PageSize=10&Vhost=push.example.net&Domain=push.example.net&App=live&Stream=stream001&Sort=desc&EndTimeFrom=2021-10-02T19:39:26+08:00&EndTimeTo=2021-10-02T19:39:26+08:00&SourceType=push&QueryType=fuzzy
        if(pageNum <1)
        {
            pageNum = 1;
        }
        if(pageSize <1)
        {
            pageSize = 10;
        }        
        com.volcengine.service.live.v20230101.LiveService service = com.volcengine.service.live.v20230101.LiveService.getInstance();
        service.setAccessKey(accessKeyId);
        service.setSecretKey(secretAccessKey);
        DescribeClosedStreamInfoByPageQuery query = new DescribeClosedStreamInfoByPageQuery();
        query.setVhost(vHost);
        query.setApp(appName);
        query.setDomain(domainName);
        if(StrUtil.isNotBlank(streamName))
        {
            query.setStream(streamName);
        }
        query.setSourceType("push");
        if(StrUtil.isNotBlank(EndTimeFrom))
        {
            query.setEndTimeFrom(EndTimeFrom);
        }
        if(StrUtil.isNotBlank(EndTimeTo))
        {
            query.setEndTimeTo(EndTimeTo);
        }
        query.setSort(sort);
        query.setPageNum(pageNum);
        query.setPageSize(pageSize);
        try 
        {
            DescribeClosedStreamInfoByPageRes resp = service.describeClosedStreamInfoByPage(query);
            System.out.println(resp);
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }        
        return BaseResult.success();
    }
    /**
     * 查询禁推流列表
     *
     * @param vHost
     * @param appName
     * @param streamName
     * @param sort  asc：升序；desc：降序。
     * @param pageNum 
     * @param pageSize 
     */
    public static BaseResult DescribeForbiddenStreamInfoByPage(String vHost,String appName,String streamName,String sort,int pageNum,int pageSize)
    {    
        //,String domainName
        //https://live.volcengineapi.com?Action=DescribeForbiddenStreamInfoByPage&Version=2023-01-01&PageNum=1&PageSize=10&Vhost=push.example.net&App=live&Stream=stream001&Sort=desc&QueryType=fuzzy
        if(pageNum <1)
        {
            pageNum = 1;
        }
        if(pageSize <1)
        {
            pageSize = 10;
        }  
        com.volcengine.service.live.v20230101.LiveService service = com.volcengine.service.live.v20230101.LiveService.getInstance();
        service.setAccessKey(accessKeyId);
        service.setSecretKey(secretAccessKey);
        DescribeForbiddenStreamInfoByPageQuery query = new DescribeForbiddenStreamInfoByPageQuery();
        query.setVhost(vHost);
        query.setApp(appName);
        if(StrUtil.isNotBlank(streamName))
        {
            query.setStream(streamName);
        }
        query.setSort(sort);
        query.setPageNum(pageNum);
        query.setPageSize(pageSize);        
        try 
        {
            DescribeForbiddenStreamInfoByPageRes resp = service.describeForbiddenStreamInfoByPage(query);
            System.out.println(resp);
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        return BaseResult.success();
    } 
}
